from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import traceback
from autoTest_ecshop.utils.log_util import logger

class Base():
    def __init__(self,driver):
        self.driver=driver
        self.wait =WebDriverWait(self.driver,10)

    # 定位单个元素
    def find_element(self,locator):
        try:
            logger.info(f'定位{locator}元素')
            return self.wait.until(EC.visibility_of_element_located(locator))

        except:
            logger.error(f'元素{locator}定位失败，失败日志信息是：\n{traceback.format_exc()}')

    # 定位一组元素
    def find_elements(self, locator):
         try:
             logger.info(f'定位{locator}元素')
             return self.wait.until(EC.visibility_of_all_elements_located(locator))

         except:
             logger.error(f'元素{locator}定位失败，失败日志信息是：\n{traceback.format_exc()}')

    # 在输入框中输入数据
    def input_data(self,locator,keyword):
        print(f'输入数据：{keyword}')
        ele=self.find_element(locator)
        ele.clear()
        ele.send_keys(keyword)


    def click_btn(self,locator):
        print(f'点击按钮：{locator}')
        btn=self.find_element(locator)
        btn.click()

    def get_text(self,locator):
        print(f'获取的文本信息：{locator}')
        ele =self.find_element(locator)
        text =ele.text
        print(f'{locator}的文本信息是：{text}')
        return text

    # 指定的页面
    def get_page(self,url):
        self.driver.get(url)